import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../data/models/project_model.dart' hide Priority;
import '../../../data/models/todo_model.dart';

/// 项目看板组件
class ProjectBoardWidget extends ConsumerWidget {
  final ProjectModel projectModel;
  final List<TodoModel> todos;
  final Function(TodoModel, TodoStatus)? onStatusChanged;

  const ProjectBoardWidget({
    super.key,
    required this.projectModel,
    required this.todos,
    this.onStatusChanged,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Column(
      children: [
        // 项目信息
        _buildProjectHeader(context),

        const SizedBox(height: 16),

        // 看板列
        Expanded(
          child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: TodoStatus.values.map((status) {
                final statusTodos =
                    todos.where((t) => t.status == status).toList();
                return _buildStatusColumn(context, status, statusTodos);
              }).toList(),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildProjectHeader(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              projectModel.projectName,
              style: Theme.of(context).textTheme.titleLarge,
            ),
            if (projectModel.description != null)
              Text(
                projectModel.description!,
                style: TextStyle(color: Colors.grey.shade600),
              ),
            const SizedBox(height: 12),
            Row(
              children: [
                Expanded(
                  child: LinearProgressIndicator(
                    value: projectModel.progressPercentage / 100,
                    backgroundColor: Colors.grey.shade300,
                  ),
                ),
                const SizedBox(width: 12),
                Text('${projectModel.progressPercentage.toInt()}%'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatusColumn(
      BuildContext context, TodoStatus status, List<TodoModel> statusTodos) {
    return Container(
      width: 280,
      margin: const EdgeInsets.only(right: 16),
      child: Column(
        children: [
          // 列头
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: _getStatusColor(status).withValues(alpha: 0.1),
              borderRadius:
                  const BorderRadius.vertical(top: Radius.circular(8)),
            ),
            child: Row(
              children: [
                Text(_getStatusText(status)),
                const Spacer(),
                Text('${statusTodos.length}'),
              ],
            ),
          ),

          // 任务列表
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                color: Colors.grey.shade50,
                borderRadius:
                    const BorderRadius.vertical(bottom: Radius.circular(8)),
              ),
              child: statusTodos.isEmpty
                  ? _buildEmptyState()
                  : ListView.builder(
                      padding: const EdgeInsets.all(8),
                      itemCount: statusTodos.length,
                      itemBuilder: (context, index) {
                        return _buildTodoCard(statusTodos[index]);
                      },
                    ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildEmptyState() {
    return const Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(Icons.inbox, size: 48, color: Colors.grey),
          SizedBox(height: 8),
          Text('暂无任务', style: TextStyle(color: Colors.grey)),
        ],
      ),
    );
  }

  Widget _buildTodoCard(TodoModel todo) {
    return Card(
      margin: const EdgeInsets.only(bottom: 8),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    todo.title,
                    style: const TextStyle(fontWeight: FontWeight.w500),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Container(
                  width: 8,
                  height: 8,
                  decoration: BoxDecoration(
                    color: _getPriorityColor(todo.priority),
                    shape: BoxShape.circle,
                  ),
                ),
              ],
            ),
            if (todo.description?.isNotEmpty == true) ...[
              const SizedBox(height: 8),
              Text(
                todo.description!,
                style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ],
            const SizedBox(height: 8),
            Row(
              children: [
                if (todo.assigneeName != null) ...[
                  CircleAvatar(
                    radius: 10,
                    backgroundColor: Colors.blue.shade100,
                    child: Text(
                      todo.assigneeName!.substring(0, 1),
                      style:
                          TextStyle(fontSize: 10, color: Colors.blue.shade700),
                    ),
                  ),
                  const SizedBox(width: 8),
                ],
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                  decoration: BoxDecoration(
                    color: _getTypeColor(todo.todoType).withValues(alpha: 0.1),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    _getTypeText(todo.todoType),
                    style: TextStyle(
                      fontSize: 10,
                      color: _getTypeColor(todo.todoType),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                const Spacer(),
                if (todo.dueDate != null)
                  Icon(
                    Icons.schedule,
                    size: 12,
                    color: todo.isOverdue ? Colors.red : Colors.grey,
                  ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Color _getStatusColor(TodoStatus status) {
    switch (status) {
      case TodoStatus.todo:
        return Colors.grey;
      case TodoStatus.inProgress:
        return Colors.blue;
      case TodoStatus.done:
        return Colors.green;
      case TodoStatus.blocked:
        return Colors.red;
    }
  }

  String _getStatusText(TodoStatus status) {
    switch (status) {
      case TodoStatus.todo:
        return '待办';
      case TodoStatus.inProgress:
        return '进行中';
      case TodoStatus.done:
        return '已完成';
      case TodoStatus.blocked:
        return '阻塞';
    }
  }

  Color _getPriorityColor(Priority priority) {
    switch (priority) {
      case Priority.low:
        return Colors.green;
      case Priority.medium:
        return Colors.orange;
      case Priority.high:
        return Colors.red;
    }
  }

  Color _getTypeColor(TodoType type) {
    switch (type) {
      case TodoType.communication:
        return Colors.blue;
      case TodoType.schedule:
        return Colors.green;
      case TodoType.document:
        return Colors.orange;
      case TodoType.dataSource:
        return Colors.purple;
      case TodoType.develop:
        return Colors.red;
      case TodoType.test:
        return Colors.teal;
      case TodoType.uat:
        return Colors.indigo;
      case TodoType.release:
        return Colors.pink;
    }
  }

  String _getTypeText(TodoType type) {
    switch (type) {
      case TodoType.communication:
        return '沟通';
      case TodoType.schedule:
        return '排期';
      case TodoType.document:
        return '文档';
      case TodoType.dataSource:
        return '数据';
      case TodoType.develop:
        return '开发';
      case TodoType.test:
        return '测试';
      case TodoType.uat:
        return 'UAT';
      case TodoType.release:
        return '发布';
    }
  }
}
